package servlet;

import domain.Student;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import util.TemplateEngineUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PushbackReader;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String username=req.getParameter("username");
       String password=req.getParameter("password");
        TemplateEngine engine= TemplateEngineUtil.getTemplateEngine(req.getServletContext());
        WebContext context = new WebContext(req, resp, req.getServletContext());
        resp.setCharacterEncoding("utf-8");
        Student student1=new Student();
        student1.setName(username);
        student1.setPassword(password);
        student1.setAge(Integer.parseInt(req.getParameter("age")));
        //deliver student object to showUsername.html
        //req.setAttribute("student",student);
         List students=new ArrayList();
         students.add(student1);
         //create student2,student3
        Student student2=new Student();
        student2.setName("smith");
        student2.setPassword("smith");
        student2.setAge(50);
        students.add(student2);
        Student student3=new Student();
        student3.setName("hela");
        student3.setPassword("hela");
        student3.setAge(500);
        students.add(student3);
         //deliver students to showUsername.html
        req.setAttribute("students",students);

        engine.process("showUsername.html",context,resp.getWriter());
//         if(username.equals("admin")&&password.equals("admin"))
//             engine.process("success.html",context,resp.getWriter());
//         else
//             engine.process("index.html",context,resp.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
